package com.gc.ordersys2.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gc.ordersys2.common.result.Result;
import com.gc.ordersys2.common.result.ResultUtils;
import com.gc.ordersys2.entity.OrderDetail;
import com.gc.ordersys2.entity.Ordered;
import com.gc.ordersys2.entity.Type;
import com.gc.ordersys2.mapper.OrderedMapper;
import com.gc.ordersys2.service.IOrderedService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuijianhao
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/ordered")
public class OrderedController {
    @Autowired
    IOrderedService service;

    @Autowired(required = false)
    OrderedMapper mapper;

    @RequestMapping("/getListAndDetail")
    public Result<Ordered> getListAndDetail() {
        MPJLambdaWrapper wrapper = new MPJLambdaWrapper<Ordered>()
                .selectAll(Ordered.class) //查询所有：订单实体类
                .selectCollection(OrderDetail.class, Ordered::getOrderDetails)
                .leftJoin(OrderDetail.class, OrderDetail::getOrderId, Ordered::getId);
        wrapper.orderByDesc("id");
        List<Ordered> list = mapper.selectJoinList(Ordered.class, wrapper);
        return ResultUtils.returnDataSuccess(list);
    }

    @RequestMapping("/getList")
    public Result<Ordered> getList(Integer page,Integer limit){
        //封装查询条件，排序方式
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("tname","爆品");
        queryWrapper.orderByDesc("id");

        //分页
        IPage<Ordered> p = new Page<>(page,limit);



        //查询---带条件分页查询
        IPage list = service.page(p, queryWrapper);
        List records = list.getRecords();   //当前页的数据
        int total = (int)list.getTotal();   //总条数

        return ResultUtils.returnSuccessLayui(records,total);
    }
    @RequestMapping("/del")
    public Object del(Integer id){
        if(service.removeById(id)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail();
        }
    }
    @RequestMapping("/add")
    public Object add(Ordered ordered){
        ordered.setOrderTime(new Date());
        if(service.save(ordered)){
            return ResultUtils.returnDataSuccess(ordered);
        }else{
            return ResultUtils.returnFail("添加失败");
        }
    }
    @RequestMapping("/submit")
    public Object submit(Integer deskId){
        if(service.submit(deskId)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail("添加失败");
        }
    }
}
